package auctions;

import jade.core.Agent;

import jade.domain.FIPANames;

import jade.lang.acl.ACLMessage;

import jade.proto.IteratedAchieveREInitiator;

import java.util.Vector;


public class EnglishSeller extends IteratedAchieveREInitiator {
    //the current price for the item
    private int price;
    //true if the licitation has finished
    private boolean done;
    
    public EnglishSeller(Agent agent, ACLMessage rMessage) {
        super(agent, rMessage);
        price = Integer.parseInt(rMessage.getContent());
        done = false;
        ((SellerAgent)myAgent).addMessage("Starting licitation.");
        ((SellerAgent)myAgent).addMessage("Starting price is " + price);
    }
    
    protected void handleAllResultNotifications(Vector notifications,
                                                Vector nextRequests) {
        if(done ) {
          //remove this agent
          myAgent.doDelete();
          return;
        }
        int count = 0;
        int nextRequestPerformative;
        String winnerName = "";
        int lastPrice = price;
        ((SellerAgent)myAgent).addMessage("Who offers more than " + price + 
                                          "?");
        for(int i = 0;i < notifications.size();i++) {
            ACLMessage m = (ACLMessage)notifications.get(i);
            //update the price and count the number of offers for that price
            if((m.getPerformative() == ACLMessage.INFORM) &&
               (!isSessionTerminated(m))) {
                int agentPrice = Integer.parseInt(m.getContent());
                if(price < agentPrice) {
                    winnerName = m.getSender().getLocalName();
                    lastPrice = price;
                    price = agentPrice;
                }
                count++;
            }
        }
     
        //if there is only one buyer left then accept the proposal
        if(count == 1) {
            ((SellerAgent)myAgent).addMessage("Item sold to " + winnerName +
                " at price " + lastPrice + ". Auction finished.");
            nextRequestPerformative = ACLMessage.ACCEPT_PROPOSAL;
            done = true;
        } else {
            ((SellerAgent)myAgent).addMessage("Agent " + winnerName 
                + " offers " + price);
            //send the next round of requests for whom offers more
            nextRequestPerformative = ACLMessage.REQUEST;
        }
        for(int i = 0;i < notifications.size();i++) {
            ACLMessage m = (ACLMessage)notifications.get(i);
            ACLMessage nextRequest = new ACLMessage(nextRequestPerformative);
            nextRequest.addReceiver(m.getSender());
            nextRequest.setProtocol(FIPANames.InteractionProtocol.
                                  FIPA_ENGLISH_AUCTION);
            if(m.getPerformative() == ACLMessage.INFORM) {
                nextRequest.setContent("" + price);
            } else {
                nextRequest.setPerformative(ACLMessage.CANCEL);
            }
            nextRequests.add(nextRequest);
        }
    }
}
